//
//  LoginViewController.m
//  chanyou
//
//  Created by leon on 15/1/7.
//  Copyright (c) 2015年 wanli. All rights reserved.
//

#import "LoginViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "UserViewController.h"
#import "Singleton.h"
#import "UIImage+Blur.h"
#import "Masonry.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface LoginViewController ()
@property (nonatomic, retain) UIAlertView *alertLogin;
@end

@implementation LoginViewController
- (void)dealloc
{
    [_reputationLabel release];
    [_userNameTextField release];
    [_passwdTextField release];
    [_sinaLoginButton release];
    [_wechatLoginButton release];
    [_qqLoginButton release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"login_backgroud_biaomian"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:255 / 255.0 green:69 / 255.0 blue:0 alpha:1]];
    self.navigationItem.title = @"登陆";
    
    [self createView];
}

- (void)backAction
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)createView
{
    UIImage *image = [[UIImage imageNamed:@"login_background.jpg"] blurredImage:0.08];
    UIImageView *backGroudImage = [[UIImageView alloc] initWithImage:image];
    backGroudImage.frame = self.view.bounds;
    backGroudImage.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:backGroudImage];
    
    UIImageView *grayImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_WIDTH / 3 - 20, SCREEN_WIDTH, SCREEN_WIDTH / 3.4)];
    grayImage.image = [UIImage imageNamed:@"login_backgroud_biaomian"];
    grayImage.alpha = 0.5;
    [self.view addSubview:grayImage];
    [grayImage release];
    
    UIImageView *userIcon = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 10, SCREEN_WIDTH / 3, 20, 20)];
    userIcon.image = [UIImage imageNamed:@"iconfont-yonghu01"];
    [self.view addSubview:userIcon];
    [userIcon release];
    
    UIImageView *passwdIcon = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 10, SCREEN_WIDTH / (3 - 0.85), 20, 20)];
    passwdIcon.image = [UIImage imageNamed:@"iconfont-mima"];
    [self.view addSubview:passwdIcon];
    [passwdIcon release];
    
    self.userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 5, SCREEN_WIDTH / 3.15, SCREEN_WIDTH / 2, 30)];
    // placeholder
    UIColor *color = [UIColor whiteColor];
    _userNameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号/邮箱" attributes:@{NSForegroundColorAttributeName: color}];
    _userNameTextField.alpha = 0.5;
    [self.view addSubview:_userNameTextField];
    
    
    self.passwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 5, SCREEN_WIDTH / (3.08 - 0.85), SCREEN_WIDTH / 2, 30)];
    _passwdTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName: color}];
    [_passwdTextField setSecureTextEntry:YES];
    _passwdTextField.alpha = 0.5;
    [self.view addSubview:_passwdTextField];
    
    self.loginButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 10, SCREEN_WIDTH / 1.5, SCREEN_WIDTH * 4 / 5, SCREEN_WIDTH / 8)];
    _loginButton.backgroundColor = [UIColor colorWithRed:56 / 255.0 green:176 / 255.0 blue:239 / 255.0 alpha:1];
    _loginButton.layer.cornerRadius = SCREEN_WIDTH / 64;
    [_loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [_loginButton setTintColor:[UIColor whiteColor]];
    _loginButton.alpha = 0.6;
    [self.view addSubview:_loginButton];
    
    
    UIImageView *textView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 1 / 5, SCREEN_WIDTH / 0.9, SCREEN_WIDTH * 3 / 5, SCREEN_WIDTH / 16)];
    textView.alpha = 0.6;
    textView.image = [UIImage imageNamed:@"textlabel"];
    [self.view addSubview:textView];
    
    self.sinaLoginButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 6, SCREEN_HEIGHT * 8 / 10, SCREEN_WIDTH / 7, SCREEN_WIDTH / 7)];
    [_sinaLoginButton setBackgroundImage:[UIImage imageNamed:@"weibologo"] forState:UIControlStateNormal];
    [_sinaLoginButton addTarget:self action:@selector(sinaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_sinaLoginButton];
    
    self.wechatLoginButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2.3, SCREEN_HEIGHT * 8 / 10, SCREEN_WIDTH / 7, SCREEN_WIDTH / 7)];
    [_wechatLoginButton setBackgroundImage:[UIImage imageNamed:@"weixinlogo"] forState:UIControlStateNormal];
    [_wechatLoginButton addTarget:self action:@selector(wechatButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_wechatLoginButton];
    
    self.qqLoginButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 1.42, SCREEN_HEIGHT * 8 / 10, SCREEN_WIDTH / 7, SCREEN_WIDTH / 7)];
    [_qqLoginButton setBackgroundImage:[UIImage imageNamed:@"qqlogo"] forState:UIControlStateNormal];
    [_qqLoginButton addTarget:self action:@selector(qqButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_qqLoginButton];
    
}

- (void)wechatButtonAction:(UIButton *)button
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"抱歉,本版本暂不支持微信登陆\n请使用微博登陆!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)qqButtonAction:(UIButton *)button
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"抱歉,本版本暂不支持微信登陆\n请使用微博登陆!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
    [alert show];
    [alert release];

}

- (void)loginAction:(UIButton *)button
{
    if (self.judge == NO) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"抱歉,本版本不提供登陆功能\n请使用第三方登陆!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

- (void)sinaButtonAction:(UIButton *)button
{
    [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
//        NSLog(@"%d",result);
        if (result) {
            NSLog(@"授权登陆成功，已获取用户信息");
//            NSString *uid = [userInfo uid];
//            NSString *nickname = [userInfo nickname];
//            NSString *profileImage = [userInfo profileImage];
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登陆成功" message:[NSString stringWithFormat:@"授权登陆成功,用户ID:%@,昵称:%@,头像:%@",uid,nickname,profileImage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//            [alert show];
            self.alertLogin = [[UIAlertView alloc] initWithTitle:nil message:@"您已成功登陆" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [_alertLogin show];
            [NSTimer scheduledTimerWithTimeInterval:1.2f target:self selector:@selector(alertLoginAction:) userInfo:nil repeats:YES];
            
            UserViewController *user = [[[UserViewController alloc] init] autorelease];
            [self.navigationController pushViewController:user animated:YES];
            self.judge = YES;
            Singleton *share = [Singleton shareInstance];
            share.logined = YES;
            
            share.loginDefault = [NSUserDefaults standardUserDefaults];
            [share.loginDefault setObject:@"yes" forKey:@"登陆状态"];
        }else{
//            NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Code4App" message:@"授权失败，请看日记错误描述" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//            [alert show];
//            [alert release];
        }
    }];
}

- (void)alertLoginAction:(id)sender
{
    [_alertLogin dismissWithClickedButtonIndex:1 animated:YES];
}

-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (self.judge == YES) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}



//-(void)reloadStateWithType:(ShareType)type{
//    //现实授权信息，包括授权ID、授权有效期等。
//    //此处可以在用户进入应用的时候直接调用，如授权信息不为空且不过期可帮用户自动实现登录。
//    id<ISSPlatformCredential> credential = [ShareSDK getCredentialWithType:type];
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TEXT_TIPS", @"提示")message:[NSString stringWithFormat:@"uid = %@\ntoken = %@\nsecret = %@\n expired = %@\nextInfo = %@",[credential uid],[credential token],[credential secret],[credential expired],[credential extInfo]]delegate:nil cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW", @"知道了")otherButtonTitles:nil];
//    [alertView show];
//    [alertView release];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
